var gulp = require("gulp");
var ejs = require("gulp-ejs");
var webserver = require("gulp-webserver");
var compass = require("gulp-for-compass");
var sass = require("gulp-sass");
var replace = require("gulp-replace");
var cssBeautify = require("gulp-cssBeautify");

var paths = {
	localImg: "http://img.local",
	serverImg: "http://img.87money.com",
	localCss: "../css/",
	serverCss: "http://img.87money.com/styles/fund/",
	localJs: "../js/",
	serverJs: "http://js.87money.com/assets/fund/",
    js: {
        source: "src/js/**/*js",
        watch: "src/js/**/*.js",
        dest: "dist/js"
    },
    sass: {
        source: "src/sass/**/*.scss",
        watch: "src/sass/**/*.scss",
        dest: "dist/css"
    },
    templates: {
        source: "src/page/**/*.ejs",
        watch: "src/page/**/*.ejs",
        dest: "dist/page"
    },
    startPage: "dist/page/index.html"
};

gulp.task("default", ["webserver", "wejs", "wcss", "wjs"]);

gulp.task("webserver", function(){
    gulp.src(".")
        .pipe(webserver({
            host: "localhost",
            port: 8000,
            livereload: true,
            directoryListing: true,
            open: paths.startPage
        }))
});

gulp.task("ejs", function(){
    gulp.src(paths.templates.source)
        .pipe(ejs())
        .pipe(gulp.dest(paths.templates.dest))
});
gulp.task("mvjs", function(){
	gulp.src(paths.js.source)
		.pipe(gulp.dest(paths.js.dest))
});

gulp.task("sass", function(){
	gulp.src(paths.sass.source)
	    .pipe(compass({
	     	sassDir: "src/sass",
		    cssDir: "dist/css",
			force: true
	     }));
});



gulp.task("wejs", function(){
    gulp.watch(paths.templates.watch,["ejs"]);
});
gulp.task("wcss", function(){
	gulp.watch(paths.sass.watch, ["sass"]);
});
gulp.task("wjs", function(){
	gulp.watch(paths.js.watch, ["mvjs"]);
});

gulp.task("build", function(){

	
	//html build
	gulp.src(paths.templates.source)
	.pipe(ejs())
	.pipe(replace(paths.localImg,paths.serverImg))
	.pipe(replace(paths.localCss,paths.serverCss))
	.pipe(replace(paths.localJs,paths.serverJs))
	.pipe(gulp.dest("build/page"));
	
	//css build
	
	gulp.src(paths.sass.source)
	    .pipe(sass({
			outputStyle: "expanded",
			sourceComments: false
		}))
		.pipe(replace(paths.localImg,paths.serverImg))
		.pipe(cssBeautify())
		.pipe(gulp.dest("build/css"));
	//mv js
	gulp.src(paths.js.source)
		.pipe(gulp.dest("build/js"));
	
});




